package ru.housewife.service.dishes;

import org.hibernate.Query;
import org.hibernate.Session;
import ru.common.hibernate.HibernateAction;
import ru.housewife.form.CookingDishRecipeForm;
import ru.housewife.model.CookingDishRecipe;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: add description
 *
 * @author Alexander Troshanin
 */
public class GetAllCommonDishes implements HibernateAction<List<CookingDishRecipeForm>> {

    @Override
    public List<CookingDishRecipeForm> execute(Session session) {

        Query q = session.createQuery("select r from CookingDishRecipe r where r.commonRecipe=true");
        List<CookingDishRecipe> lst = q.list();
        List<CookingDishRecipeForm> result = new ArrayList<CookingDishRecipeForm>(lst.size());

        for (CookingDishRecipe recipe : lst) {
            CookingDishRecipeForm recipeForm = new CookingDishRecipeForm(recipe);
            recipeForm.getGroup().addRecipe(recipeForm);
            result.add(recipeForm);
        }

        return result;
    }
}
